package com.example.mydataapplication;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;

public class Sharedpre extends Fragment {
    public Sharedpre(){
        super(R.layout.sharedpre);
    }
private EditText[] editTexts;
    private final int EditCount=2;
    private int[] EditNum=new int[]{R.id.et_number,R.id.et_password};
    private  String[] EditValueKey=new String[]{"account","password"};

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        SharedPreferences sp=getActivity().getPreferences(Context.MODE_PRIVATE);
        editTexts=new EditText[EditCount];
        for (int i=0;i<EditCount;i++)
            editTexts[i]=getView().findViewById(EditNum[i]);
        getView().findViewById(R.id.bo_ne).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor=sp.edit();
                editor.putString(EditValueKey[0],editTexts[0].getText().toString());
                editor.putString(EditValueKey[1],getSHA256(editTexts[1].getText().toString()));
                editor.commit();
                Toast.makeText(getContext(),"保存成功",Toast.LENGTH_SHORT).show();
            }
        });
        for (int i=0;i<EditCount;i++)
        editTexts[i].setText(sp.getString(EditValueKey[i], ""));
    }



    private String getSHA256(String str){
        MessageDigest messageDigest;
        String encodeStr="";
        try {
            messageDigest=MessageDigest.getInstance("ShA-256");
            messageDigest.update(str.getBytes("UTF-8"));
            encodeStr=byteHex((messageDigest.digest()));
        }catch (Exception e){
            e.printStackTrace();
        }
        return encodeStr;
    }
    private String byteHex(byte[] bytes){
        StringBuffer stringBuffer=new StringBuffer();
        String temp=null;
        for (int i=0;i<bytes.length;i++){
            temp=Integer.toHexString(bytes[i]&0xFF);
            if (temp.length()==1)
                stringBuffer.append("0");
                stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }
}
  /*  private EditText etNumber;
    private EditText etpassword;
    private Button btn;
    public static boolean saveUserInfo(Context context, String number, String password)
    {
        try{
            FileOutputStream fos=context.openFileOutput("Sharedpre.txt",Context.MODE_PRIVATE);
            fos.write((number+":"+password).getBytes());
            fos.close();
            return true;
        }catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
    }
    public static Map<String, String> getUserInfo(Context context)
    {
        String content="";
        try {
            FileInputStream fis=context.openFileInput("Sharedpre.txt");
            byte[] buffer=new byte[fis.available()];
            fis.read(buffer);
            content=new String(buffer);
            Map<String,String> userMap=new HashMap<String, String>();
            String[] infos=content.split(":");
            userMap.put("number",infos[0]);
            userMap.put("password",infos[1]);
            fis.close();
            return userMap;
        }
        catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState){
  initView();
    Map<String,String> userInfo=Sharedpre.getUserInfo(getActivity());
        if(userInfo!=null)
                {
                etNumber.setText(userInfo.get("number"));
                etpassword.setText(userInfo.get("password"));
                }
        String number=etNumber.getText().toString().trim();
        String password=etpassword.getText().toString();
        if (TextUtils.isEmpty(number)){
            Toast.makeText(getActivity(),"请输入QQ账号",Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(password)){
            Toast.makeText(getActivity(),"请输入QQ密码",Toast.LENGTH_SHORT).show();
            return;
        }
        Toast.makeText(getActivity(),"登录成功",Toast.LENGTH_SHORT).show();
        boolean isSaveSuccess= Sharedpre.saveUserInfo(getActivity(),number,password);
        if (isSaveSuccess)
        {
            Toast.makeText(getActivity(),"保存成功",Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(getActivity(),"保存失败",Toast.LENGTH_SHORT).show();
        }}
  private void initView(){
      etNumber=(EditText)getView().findViewById(R.id.et_number);
      etpassword=(EditText)getView().findViewById(R.id.et_password);
      btn=(Button)getView().findViewById(R.id.bo_ne);
      btn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              boolean isSaveSuccess= Sharedpre.saveUserInfo(getActivity(),Sharedpre.getUserInfo(etNumber),password);
              if (isSaveSuccess)
              {
                  Toast.makeText(getActivity(),"保存成功",Toast.LENGTH_SHORT).show();
              }else {
                  Toast.makeText(getActivity(),"保存失败",Toast.LENGTH_SHORT).show();
              }
          }
      });
  }
}*/

